﻿/* 
 * Reads the configuration file by using Linq to XML
 * Checks are very loose
 */
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace PeoneFS
{
    class Configuration
    {
        public struct MappedFolder
        {
            public string VirtualName;
            public string RealName;
        }

        public static string Password { get; private set; }
        public static char DriveLetter { get; private set; }
        public static ReadOnlyCollection<MappedFolder> MappedFolders;

        private static Configuration instance = new Configuration();


        private Configuration()
        {
            var folderName = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            XDocument doc = XDocument.Load(Path.Combine(folderName, "config.xml"));
            
            Password = doc.Root.Attribute("password").Value;
            string dl = doc.Root.Attribute("driveletter").Value;
            DriveLetter = String.IsNullOrWhiteSpace(dl) ? 'p' : dl[0];

            var folders = from e in doc.Root.Descendants("mappedFolders").Descendants("mappedFolder")
                          select new MappedFolder { VirtualName = e.Value, RealName = (string)e.Attribute("source") };


            MappedFolders = new ReadOnlyCollection<MappedFolder>(folders.ToList());
        }
    }
}
